﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration


Public Class Connect
    Private stconnect As String = ConfigurationManager.AppSettings("stconnect").ToString
    Private conn As SqlConnection
    Private cmd As SqlCommand

    Public Function Connect() As Boolean
        conn = New SqlConnection(stconnect)
        Try
            conn.Open()
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show("Khong the ket noi database! " + ex.Message)
        End Try
        Return True
    End Function

    Public Sub Close()
        If conn.State = ConnectionState.Open Then
            conn.Close()
        End If
        conn.Dispose()
    End Sub

    Public Function Execute(ByVal sSql As String)
        cmd = New SqlCommand()
        Connect()
        cmd.Connection() = conn
        cmd.CommandText = sSql
        Return cmd.ExecuteNonQuery()
    End Function

    Public Function GetData(ByVal sSql As String) As DataTable
        Dim dt As New DataTable
        Connect()
        cmd = New SqlCommand(sSql, conn)
        Dim da As New SqlDataAdapter(cmd)
        da.Fill(dt)
        Close()
        Return dt
    End Function

    End Class
